{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 删除空值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>编号</th>\n",
       "      <th>性别</th>\n",
       "      <th>出生年份</th>\n",
       "      <th>体重指数</th>\n",
       "      <th>糖尿病家族史</th>\n",
       "      <th>舒张压</th>\n",
       "      <th>口服耐糖量测试</th>\n",
       "      <th>胰岛素释放实验</th>\n",
       "      <th>肱三头肌皮褶厚度</th>\n",
       "      <th>患有糖尿病标识</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1996</td>\n",
       "      <td>30.1</td>\n",
       "      <td>无记录</td>\n",
       "      <td>106.0</td>\n",
       "      <td>3.818</td>\n",
       "      <td>7.89</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>1988</td>\n",
       "      <td>27.5</td>\n",
       "      <td>无记录</td>\n",
       "      <td>84.0</td>\n",
       "      <td>-1.000</td>\n",
       "      <td>0.00</td>\n",
       "      <td>14.7</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>1988</td>\n",
       "      <td>36.5</td>\n",
       "      <td>无记录</td>\n",
       "      <td>85.0</td>\n",
       "      <td>7.131</td>\n",
       "      <td>0.00</td>\n",
       "      <td>40.1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>1992</td>\n",
       "      <td>29.5</td>\n",
       "      <td>无记录</td>\n",
       "      <td>91.0</td>\n",
       "      <td>7.041</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>0</td>\n",
       "      <td>1998</td>\n",
       "      <td>42.0</td>\n",
       "      <td>叔叔或者姑姑有一方患有糖尿病</td>\n",
       "      <td>NaN</td>\n",
       "      <td>7.134</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   编号  性别  出生年份  体重指数          糖尿病家族史    舒张压  口服耐糖量测试  胰岛素释放实验  肱三头肌皮褶厚度  \\\n",
       "0   1   0  1996  30.1             无记录  106.0    3.818     7.89       0.0   \n",
       "1   2   0  1988  27.5             无记录   84.0   -1.000     0.00      14.7   \n",
       "2   3   1  1988  36.5             无记录   85.0    7.131     0.00      40.1   \n",
       "3   4   1  1992  29.5             无记录   91.0    7.041     0.00       0.0   \n",
       "4   5   0  1998  42.0  叔叔或者姑姑有一方患有糖尿病    NaN    7.134     0.00       0.0   \n",
       "\n",
       "   患有糖尿病标识  \n",
       "0        0  \n",
       "1        0  \n",
       "2        1  \n",
       "3        0  \n",
       "4        1  "
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.read_csv(r'./data/test_2022.csv', encoding = 'utf-8')\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(5070, 10)"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "((4823, 10), (5070, 10))"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.dropna(axis=0).shape,df.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "((5070, 9), (5070, 10))"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.dropna(axis=1).shape, df.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(5070, 10)"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.dropna(axis=1,how=\"all\").shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 5070 entries, 0 to 5069\n",
      "Data columns (total 10 columns):\n",
      " #   Column    Non-Null Count  Dtype  \n",
      "---  ------    --------------  -----  \n",
      " 0   编号        5070 non-null   int64  \n",
      " 1   性别        5070 non-null   int64  \n",
      " 2   出生年份      5070 non-null   int64  \n",
      " 3   体重指数      5070 non-null   float64\n",
      " 4   糖尿病家族史    5070 non-null   object \n",
      " 5   舒张压       4823 non-null   float64\n",
      " 6   口服耐糖量测试   5070 non-null   float64\n",
      " 7   胰岛素释放实验   5070 non-null   float64\n",
      " 8   肱三头肌皮褶厚度  5070 non-null   float64\n",
      " 9   患有糖尿病标识   5070 non-null   int64  \n",
      "dtypes: float64(5), int64(4), object(1)\n",
      "memory usage: 396.2+ KB\n"
     ]
    }
   ],
   "source": [
    "df.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "247"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['舒张压'].isnull().sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['舒张压'] = df['舒张压'].fillna(df['舒张压'].median())\n",
    "df['舒张压'].isnull().sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>编号</th>\n",
       "      <th>性别</th>\n",
       "      <th>出生年份</th>\n",
       "      <th>体重指数</th>\n",
       "      <th>舒张压</th>\n",
       "      <th>口服耐糖量测试</th>\n",
       "      <th>胰岛素释放实验</th>\n",
       "      <th>肱三头肌皮褶厚度</th>\n",
       "      <th>患有糖尿病标识</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>5070.000000</td>\n",
       "      <td>5070.000000</td>\n",
       "      <td>5070.000000</td>\n",
       "      <td>5070.000000</td>\n",
       "      <td>5070.000000</td>\n",
       "      <td>5070.000000</td>\n",
       "      <td>5070.000000</td>\n",
       "      <td>5070.000000</td>\n",
       "      <td>5070.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>2535.500000</td>\n",
       "      <td>0.456805</td>\n",
       "      <td>1986.869231</td>\n",
       "      <td>37.986785</td>\n",
       "      <td>89.402959</td>\n",
       "      <td>5.612839</td>\n",
       "      <td>4.114321</td>\n",
       "      <td>6.994371</td>\n",
       "      <td>0.381854</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>1463.727263</td>\n",
       "      <td>0.498180</td>\n",
       "      <td>8.919737</td>\n",
       "      <td>11.447095</td>\n",
       "      <td>9.038854</td>\n",
       "      <td>2.257649</td>\n",
       "      <td>8.726001</td>\n",
       "      <td>13.651442</td>\n",
       "      <td>0.485889</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1943.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>30.000000</td>\n",
       "      <td>-1.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>1268.250000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1980.000000</td>\n",
       "      <td>28.400000</td>\n",
       "      <td>85.000000</td>\n",
       "      <td>4.314000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>2535.500000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1987.000000</td>\n",
       "      <td>36.550000</td>\n",
       "      <td>89.000000</td>\n",
       "      <td>5.760000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>3802.750000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1995.000000</td>\n",
       "      <td>47.600000</td>\n",
       "      <td>95.000000</td>\n",
       "      <td>7.193000</td>\n",
       "      <td>7.100000</td>\n",
       "      <td>4.120000</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>5070.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>2009.000000</td>\n",
       "      <td>65.900000</td>\n",
       "      <td>126.000000</td>\n",
       "      <td>10.839000</td>\n",
       "      <td>108.960000</td>\n",
       "      <td>45.000000</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                编号           性别         出生年份         体重指数          舒张压  \\\n",
       "count  5070.000000  5070.000000  5070.000000  5070.000000  5070.000000   \n",
       "mean   2535.500000     0.456805  1986.869231    37.986785    89.402959   \n",
       "std    1463.727263     0.498180     8.919737    11.447095     9.038854   \n",
       "min       1.000000     0.000000  1943.000000     0.000000    30.000000   \n",
       "25%    1268.250000     0.000000  1980.000000    28.400000    85.000000   \n",
       "50%    2535.500000     0.000000  1987.000000    36.550000    89.000000   \n",
       "75%    3802.750000     1.000000  1995.000000    47.600000    95.000000   \n",
       "max    5070.000000     1.000000  2009.000000    65.900000   126.000000   \n",
       "\n",
       "           口服耐糖量测试      胰岛素释放实验     肱三头肌皮褶厚度      患有糖尿病标识  \n",
       "count  5070.000000  5070.000000  5070.000000  5070.000000  \n",
       "mean      5.612839     4.114321     6.994371     0.381854  \n",
       "std       2.257649     8.726001    13.651442     0.485889  \n",
       "min      -1.000000     0.000000     0.000000     0.000000  \n",
       "25%       4.314000     0.000000     0.000000     0.000000  \n",
       "50%       5.760000     0.000000     0.000000     0.000000  \n",
       "75%       7.193000     7.100000     4.120000     1.000000  \n",
       "max      10.839000   108.960000    45.000000     1.000000  "
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(0.0, 65.9, 37.98678500986196)"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[\"体重指数\"].min(), df[\"体重指数\"].max(), df[\"体重指数\"].mean(), "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "体重指数     169\n",
       "舒张压     2235\n",
       "dtype: int64"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#值的最小值序号\n",
    "df[[\"体重指数\",\"舒张压\"]].idxmin()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "体重指数    36.55\n",
       "舒张压     89.00\n",
       "dtype: float64"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#值的中位数\n",
    "df[[\"体重指数\",\"舒张压\"]].median()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "体重指数    9.872053\n",
       "舒张压     6.344196\n",
       "dtype: float64"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#计算与平均值的平均距离差\n",
    "df[[\"体重指数\",\"舒张压\"]].mad()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "体重指数    131.035974\n",
       "舒张压      81.700879\n",
       "dtype: float64"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#计算样本值的方差\n",
    "df[[\"体重指数\",\"舒张压\"]].var()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array(['无记录', '叔叔或者姑姑有一方患有糖尿病', '叔叔或姑姑有一方患有糖尿病', '父母有一方患有糖尿病'],\n",
       "      dtype=object)"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 统计唯一值\n",
    "df[\"糖尿病家族史\"].unique()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    2754\n",
       "1    2316\n",
       "Name: 性别, dtype: int64"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 统计各值的数量\n",
    "df[\"性别\"].value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>编号</th>\n",
       "      <th>性别</th>\n",
       "      <th>出生年份</th>\n",
       "      <th>体重指数</th>\n",
       "      <th>糖尿病家族史</th>\n",
       "      <th>舒张压</th>\n",
       "      <th>口服耐糖量测试</th>\n",
       "      <th>胰岛素释放实验</th>\n",
       "      <th>肱三头肌皮褶厚度</th>\n",
       "      <th>患有糖尿病标识</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1996</td>\n",
       "      <td>30.1</td>\n",
       "      <td>无记录</td>\n",
       "      <td>106.0</td>\n",
       "      <td>3.818</td>\n",
       "      <td>7.89</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>1988</td>\n",
       "      <td>27.5</td>\n",
       "      <td>无记录</td>\n",
       "      <td>84.0</td>\n",
       "      <td>-1.000</td>\n",
       "      <td>0.00</td>\n",
       "      <td>14.7</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>1988</td>\n",
       "      <td>36.5</td>\n",
       "      <td>无记录</td>\n",
       "      <td>85.0</td>\n",
       "      <td>7.131</td>\n",
       "      <td>0.00</td>\n",
       "      <td>40.1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>1992</td>\n",
       "      <td>29.5</td>\n",
       "      <td>无记录</td>\n",
       "      <td>91.0</td>\n",
       "      <td>7.041</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>0</td>\n",
       "      <td>1998</td>\n",
       "      <td>42.0</td>\n",
       "      <td>叔叔或者姑姑有一方患有糖尿病</td>\n",
       "      <td>89.0</td>\n",
       "      <td>7.134</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   编号  性别  出生年份  体重指数          糖尿病家族史    舒张压  口服耐糖量测试  胰岛素释放实验  肱三头肌皮褶厚度  \\\n",
       "0   1   0  1996  30.1             无记录  106.0    3.818     7.89       0.0   \n",
       "1   2   0  1988  27.5             无记录   84.0   -1.000     0.00      14.7   \n",
       "2   3   1  1988  36.5             无记录   85.0    7.131     0.00      40.1   \n",
       "3   4   1  1992  29.5             无记录   91.0    7.041     0.00       0.0   \n",
       "4   5   0  1998  42.0  叔叔或者姑姑有一方患有糖尿病   89.0    7.134     0.00       0.0   \n",
       "\n",
       "   患有糖尿病标识  \n",
       "0        0  \n",
       "1        0  \n",
       "2        1  \n",
       "3        0  \n",
       "4        1  "
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>编号</th>\n",
       "      <th>性别</th>\n",
       "      <th>出生年份</th>\n",
       "      <th>体重指数</th>\n",
       "      <th>舒张压</th>\n",
       "      <th>口服耐糖量测试</th>\n",
       "      <th>胰岛素释放实验</th>\n",
       "      <th>肱三头肌皮褶厚度</th>\n",
       "      <th>患有糖尿病标识</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>编号</th>\n",
       "      <td>2.142498e+06</td>\n",
       "      <td>4.814954</td>\n",
       "      <td>-87.382817</td>\n",
       "      <td>0.469580</td>\n",
       "      <td>46.860426</td>\n",
       "      <td>-19.300336</td>\n",
       "      <td>261.084033</td>\n",
       "      <td>606.052606</td>\n",
       "      <td>19.511738</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>性别</th>\n",
       "      <td>4.814954e+00</td>\n",
       "      <td>0.248183</td>\n",
       "      <td>-0.531296</td>\n",
       "      <td>0.428764</td>\n",
       "      <td>0.345778</td>\n",
       "      <td>0.012892</td>\n",
       "      <td>-0.232992</td>\n",
       "      <td>0.095466</td>\n",
       "      <td>0.007620</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>出生年份</th>\n",
       "      <td>-8.738282e+01</td>\n",
       "      <td>-0.531296</td>\n",
       "      <td>79.561708</td>\n",
       "      <td>-7.617392</td>\n",
       "      <td>-12.163314</td>\n",
       "      <td>0.041977</td>\n",
       "      <td>4.559918</td>\n",
       "      <td>-1.596459</td>\n",
       "      <td>-0.295686</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>体重指数</th>\n",
       "      <td>4.695798e-01</td>\n",
       "      <td>0.428764</td>\n",
       "      <td>-7.617392</td>\n",
       "      <td>131.035974</td>\n",
       "      <td>16.134208</td>\n",
       "      <td>-0.046426</td>\n",
       "      <td>-3.446766</td>\n",
       "      <td>4.113113</td>\n",
       "      <td>2.101989</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>舒张压</th>\n",
       "      <td>4.686043e+01</td>\n",
       "      <td>0.345778</td>\n",
       "      <td>-12.163314</td>\n",
       "      <td>16.134208</td>\n",
       "      <td>81.700879</td>\n",
       "      <td>-0.409737</td>\n",
       "      <td>-16.079755</td>\n",
       "      <td>9.231805</td>\n",
       "      <td>0.677229</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>口服耐糖量测试</th>\n",
       "      <td>-1.930034e+01</td>\n",
       "      <td>0.012892</td>\n",
       "      <td>0.041977</td>\n",
       "      <td>-0.046426</td>\n",
       "      <td>-0.409737</td>\n",
       "      <td>5.096979</td>\n",
       "      <td>1.846212</td>\n",
       "      <td>-0.199822</td>\n",
       "      <td>0.195406</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>胰岛素释放实验</th>\n",
       "      <td>2.610840e+02</td>\n",
       "      <td>-0.232992</td>\n",
       "      <td>4.559918</td>\n",
       "      <td>-3.446766</td>\n",
       "      <td>-16.079755</td>\n",
       "      <td>1.846212</td>\n",
       "      <td>76.143097</td>\n",
       "      <td>-1.843871</td>\n",
       "      <td>0.664199</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>肱三头肌皮褶厚度</th>\n",
       "      <td>6.060526e+02</td>\n",
       "      <td>0.095466</td>\n",
       "      <td>-1.596459</td>\n",
       "      <td>4.113113</td>\n",
       "      <td>9.231805</td>\n",
       "      <td>-0.199822</td>\n",
       "      <td>-1.843871</td>\n",
       "      <td>186.361872</td>\n",
       "      <td>2.723987</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>患有糖尿病标识</th>\n",
       "      <td>1.951174e+01</td>\n",
       "      <td>0.007620</td>\n",
       "      <td>-0.295686</td>\n",
       "      <td>2.101989</td>\n",
       "      <td>0.677229</td>\n",
       "      <td>0.195406</td>\n",
       "      <td>0.664199</td>\n",
       "      <td>2.723987</td>\n",
       "      <td>0.236088</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                    编号        性别       出生年份        体重指数        舒张压    口服耐糖量测试  \\\n",
       "编号        2.142498e+06  4.814954 -87.382817    0.469580  46.860426 -19.300336   \n",
       "性别        4.814954e+00  0.248183  -0.531296    0.428764   0.345778   0.012892   \n",
       "出生年份     -8.738282e+01 -0.531296  79.561708   -7.617392 -12.163314   0.041977   \n",
       "体重指数      4.695798e-01  0.428764  -7.617392  131.035974  16.134208  -0.046426   \n",
       "舒张压       4.686043e+01  0.345778 -12.163314   16.134208  81.700879  -0.409737   \n",
       "口服耐糖量测试  -1.930034e+01  0.012892   0.041977   -0.046426  -0.409737   5.096979   \n",
       "胰岛素释放实验   2.610840e+02 -0.232992   4.559918   -3.446766 -16.079755   1.846212   \n",
       "肱三头肌皮褶厚度  6.060526e+02  0.095466  -1.596459    4.113113   9.231805  -0.199822   \n",
       "患有糖尿病标识   1.951174e+01  0.007620  -0.295686    2.101989   0.677229   0.195406   \n",
       "\n",
       "             胰岛素释放实验    肱三头肌皮褶厚度    患有糖尿病标识  \n",
       "编号        261.084033  606.052606  19.511738  \n",
       "性别         -0.232992    0.095466   0.007620  \n",
       "出生年份        4.559918   -1.596459  -0.295686  \n",
       "体重指数       -3.446766    4.113113   2.101989  \n",
       "舒张压       -16.079755    9.231805   0.677229  \n",
       "口服耐糖量测试     1.846212   -0.199822   0.195406  \n",
       "胰岛素释放实验    76.143097   -1.843871   0.664199  \n",
       "肱三头肌皮褶厚度   -1.843871  186.361872   2.723987  \n",
       "患有糖尿病标识     0.664199    2.723987   0.236088  "
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 协防差矩阵\n",
    "df.cov()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>编号</th>\n",
       "      <th>性别</th>\n",
       "      <th>出生年份</th>\n",
       "      <th>体重指数</th>\n",
       "      <th>舒张压</th>\n",
       "      <th>口服耐糖量测试</th>\n",
       "      <th>胰岛素释放实验</th>\n",
       "      <th>肱三头肌皮褶厚度</th>\n",
       "      <th>患有糖尿病标识</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>编号</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.006603</td>\n",
       "      <td>-0.006693</td>\n",
       "      <td>0.000028</td>\n",
       "      <td>0.003542</td>\n",
       "      <td>-0.005840</td>\n",
       "      <td>0.020441</td>\n",
       "      <td>0.030330</td>\n",
       "      <td>0.027435</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>性别</th>\n",
       "      <td>0.006603</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>-0.119563</td>\n",
       "      <td>0.075186</td>\n",
       "      <td>0.076789</td>\n",
       "      <td>0.011463</td>\n",
       "      <td>-0.053597</td>\n",
       "      <td>0.014037</td>\n",
       "      <td>0.031480</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>出生年份</th>\n",
       "      <td>-0.006693</td>\n",
       "      <td>-0.119563</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>-0.074603</td>\n",
       "      <td>-0.150864</td>\n",
       "      <td>0.002085</td>\n",
       "      <td>0.058585</td>\n",
       "      <td>-0.013111</td>\n",
       "      <td>-0.068225</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>体重指数</th>\n",
       "      <td>0.000028</td>\n",
       "      <td>0.075186</td>\n",
       "      <td>-0.074603</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.155933</td>\n",
       "      <td>-0.001796</td>\n",
       "      <td>-0.034507</td>\n",
       "      <td>0.026321</td>\n",
       "      <td>0.377919</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>舒张压</th>\n",
       "      <td>0.003542</td>\n",
       "      <td>0.076789</td>\n",
       "      <td>-0.150864</td>\n",
       "      <td>0.155933</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>-0.020079</td>\n",
       "      <td>-0.203869</td>\n",
       "      <td>0.074816</td>\n",
       "      <td>0.154200</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>口服耐糖量测试</th>\n",
       "      <td>-0.005840</td>\n",
       "      <td>0.011463</td>\n",
       "      <td>0.002085</td>\n",
       "      <td>-0.001796</td>\n",
       "      <td>-0.020079</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.093715</td>\n",
       "      <td>-0.006483</td>\n",
       "      <td>0.178133</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>胰岛素释放实验</th>\n",
       "      <td>0.020441</td>\n",
       "      <td>-0.053597</td>\n",
       "      <td>0.058585</td>\n",
       "      <td>-0.034507</td>\n",
       "      <td>-0.203869</td>\n",
       "      <td>0.093715</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>-0.015479</td>\n",
       "      <td>0.156656</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>肱三头肌皮褶厚度</th>\n",
       "      <td>0.030330</td>\n",
       "      <td>0.014037</td>\n",
       "      <td>-0.013111</td>\n",
       "      <td>0.026321</td>\n",
       "      <td>0.074816</td>\n",
       "      <td>-0.006483</td>\n",
       "      <td>-0.015479</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.410667</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>患有糖尿病标识</th>\n",
       "      <td>0.027435</td>\n",
       "      <td>0.031480</td>\n",
       "      <td>-0.068225</td>\n",
       "      <td>0.377919</td>\n",
       "      <td>0.154200</td>\n",
       "      <td>0.178133</td>\n",
       "      <td>0.156656</td>\n",
       "      <td>0.410667</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                编号        性别      出生年份      体重指数       舒张压   口服耐糖量测试  \\\n",
       "编号        1.000000  0.006603 -0.006693  0.000028  0.003542 -0.005840   \n",
       "性别        0.006603  1.000000 -0.119563  0.075186  0.076789  0.011463   \n",
       "出生年份     -0.006693 -0.119563  1.000000 -0.074603 -0.150864  0.002085   \n",
       "体重指数      0.000028  0.075186 -0.074603  1.000000  0.155933 -0.001796   \n",
       "舒张压       0.003542  0.076789 -0.150864  0.155933  1.000000 -0.020079   \n",
       "口服耐糖量测试  -0.005840  0.011463  0.002085 -0.001796 -0.020079  1.000000   \n",
       "胰岛素释放实验   0.020441 -0.053597  0.058585 -0.034507 -0.203869  0.093715   \n",
       "肱三头肌皮褶厚度  0.030330  0.014037 -0.013111  0.026321  0.074816 -0.006483   \n",
       "患有糖尿病标识   0.027435  0.031480 -0.068225  0.377919  0.154200  0.178133   \n",
       "\n",
       "           胰岛素释放实验  肱三头肌皮褶厚度   患有糖尿病标识  \n",
       "编号        0.020441  0.030330  0.027435  \n",
       "性别       -0.053597  0.014037  0.031480  \n",
       "出生年份      0.058585 -0.013111 -0.068225  \n",
       "体重指数     -0.034507  0.026321  0.377919  \n",
       "舒张压      -0.203869  0.074816  0.154200  \n",
       "口服耐糖量测试   0.093715 -0.006483  0.178133  \n",
       "胰岛素释放实验   1.000000 -0.015479  0.156656  \n",
       "肱三头肌皮褶厚度 -0.015479  1.000000  0.410667  \n",
       "患有糖尿病标识   0.156656  0.410667  1.000000  "
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 相关系数矩阵\n",
    "df.corr()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.37791854250960205"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[\"体重指数\"].corr(df[\"患有糖尿病标识\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 定义BMI指数\n",
    "def BMI(x):\n",
    "    if x < 18.5:\n",
    "        return 0\n",
    "    elif 18.5 <= x <=23.9:\n",
    "        return 1\n",
    "    elif 24 <= x <= 27.9:\n",
    "        return 2\n",
    "    elif 28 <= x <= 29.9:\n",
    "        return 3\n",
    "    elif x >= 30:\n",
    "        return 4\n",
    "    \n",
    "df['体质指数-BMI'] = df['体重指数'].apply(BMI)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.17620834815562078"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[\"体质指数-BMI\"].corr(df[\"患有糖尿病标识\"])  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "## 排序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>编号</th>\n",
       "      <th>性别</th>\n",
       "      <th>出生年份</th>\n",
       "      <th>体重指数</th>\n",
       "      <th>糖尿病家族史</th>\n",
       "      <th>舒张压</th>\n",
       "      <th>口服耐糖量测试</th>\n",
       "      <th>胰岛素释放实验</th>\n",
       "      <th>肱三头肌皮褶厚度</th>\n",
       "      <th>患有糖尿病标识</th>\n",
       "      <th>体质指数-BMI</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1996</td>\n",
       "      <td>30.1</td>\n",
       "      <td>无记录</td>\n",
       "      <td>106.0</td>\n",
       "      <td>3.818</td>\n",
       "      <td>7.89</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>1988</td>\n",
       "      <td>27.5</td>\n",
       "      <td>无记录</td>\n",
       "      <td>84.0</td>\n",
       "      <td>-1.000</td>\n",
       "      <td>0.00</td>\n",
       "      <td>14.7</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   编号  性别  出生年份  体重指数 糖尿病家族史    舒张压  口服耐糖量测试  胰岛素释放实验  肱三头肌皮褶厚度  患有糖尿病标识  \\\n",
       "0   1   0  1996  30.1    无记录  106.0    3.818     7.89       0.0        0   \n",
       "1   2   0  1988  27.5    无记录   84.0   -1.000     0.00      14.7        0   \n",
       "\n",
       "   体质指数-BMI  \n",
       "0         4  \n",
       "1         2  "
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.head(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2235     30.0\n",
       "4327     34.0\n",
       "612      44.0\n",
       "4874     44.0\n",
       "3740     44.0\n",
       "        ...  \n",
       "3914    112.0\n",
       "356     114.0\n",
       "2982    116.0\n",
       "4817    120.0\n",
       "361     126.0\n",
       "Name: 舒张压, Length: 5070, dtype: float64"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[\"舒张压\"].sort_values()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>编号</th>\n",
       "      <th>性别</th>\n",
       "      <th>出生年份</th>\n",
       "      <th>体重指数</th>\n",
       "      <th>糖尿病家族史</th>\n",
       "      <th>舒张压</th>\n",
       "      <th>口服耐糖量测试</th>\n",
       "      <th>胰岛素释放实验</th>\n",
       "      <th>肱三头肌皮褶厚度</th>\n",
       "      <th>患有糖尿病标识</th>\n",
       "      <th>体质指数-BMI</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2135</th>\n",
       "      <td>2136</td>\n",
       "      <td>0</td>\n",
       "      <td>1998</td>\n",
       "      <td>65.9</td>\n",
       "      <td>叔叔或者姑姑有一方患有糖尿病</td>\n",
       "      <td>108.0</td>\n",
       "      <td>7.027</td>\n",
       "      <td>19.03</td>\n",
       "      <td>4.51</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1097</th>\n",
       "      <td>1098</td>\n",
       "      <td>0</td>\n",
       "      <td>1976</td>\n",
       "      <td>60.0</td>\n",
       "      <td>叔叔或姑姑有一方患有糖尿病</td>\n",
       "      <td>83.0</td>\n",
       "      <td>3.288</td>\n",
       "      <td>0.00</td>\n",
       "      <td>20.00</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1013</th>\n",
       "      <td>1014</td>\n",
       "      <td>0</td>\n",
       "      <td>2000</td>\n",
       "      <td>60.0</td>\n",
       "      <td>无记录</td>\n",
       "      <td>87.0</td>\n",
       "      <td>7.291</td>\n",
       "      <td>9.87</td>\n",
       "      <td>0.00</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>479</th>\n",
       "      <td>480</td>\n",
       "      <td>1</td>\n",
       "      <td>1997</td>\n",
       "      <td>60.0</td>\n",
       "      <td>无记录</td>\n",
       "      <td>88.0</td>\n",
       "      <td>4.194</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1799</th>\n",
       "      <td>1800</td>\n",
       "      <td>0</td>\n",
       "      <td>1975</td>\n",
       "      <td>60.0</td>\n",
       "      <td>无记录</td>\n",
       "      <td>97.0</td>\n",
       "      <td>5.495</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4458</th>\n",
       "      <td>4459</td>\n",
       "      <td>0</td>\n",
       "      <td>2002</td>\n",
       "      <td>0.0</td>\n",
       "      <td>无记录</td>\n",
       "      <td>89.0</td>\n",
       "      <td>4.351</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>169</th>\n",
       "      <td>170</td>\n",
       "      <td>1</td>\n",
       "      <td>1998</td>\n",
       "      <td>0.0</td>\n",
       "      <td>无记录</td>\n",
       "      <td>89.0</td>\n",
       "      <td>6.203</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3243</th>\n",
       "      <td>3244</td>\n",
       "      <td>1</td>\n",
       "      <td>1967</td>\n",
       "      <td>0.0</td>\n",
       "      <td>无记录</td>\n",
       "      <td>100.0</td>\n",
       "      <td>6.801</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4488</th>\n",
       "      <td>4489</td>\n",
       "      <td>0</td>\n",
       "      <td>1951</td>\n",
       "      <td>0.0</td>\n",
       "      <td>父母有一方患有糖尿病</td>\n",
       "      <td>88.0</td>\n",
       "      <td>7.409</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3062</th>\n",
       "      <td>3063</td>\n",
       "      <td>0</td>\n",
       "      <td>2003</td>\n",
       "      <td>0.0</td>\n",
       "      <td>叔叔或者姑姑有一方患有糖尿病</td>\n",
       "      <td>89.0</td>\n",
       "      <td>4.577</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5070 rows × 11 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "        编号  性别  出生年份  体重指数          糖尿病家族史    舒张压  口服耐糖量测试  胰岛素释放实验  肱三头肌皮褶厚度  \\\n",
       "2135  2136   0  1998  65.9  叔叔或者姑姑有一方患有糖尿病  108.0    7.027    19.03      4.51   \n",
       "1097  1098   0  1976  60.0   叔叔或姑姑有一方患有糖尿病   83.0    3.288     0.00     20.00   \n",
       "1013  1014   0  2000  60.0             无记录   87.0    7.291     9.87      0.00   \n",
       "479    480   1  1997  60.0             无记录   88.0    4.194     0.00      0.00   \n",
       "1799  1800   0  1975  60.0             无记录   97.0    5.495     0.00      0.00   \n",
       "...    ...  ..   ...   ...             ...    ...      ...      ...       ...   \n",
       "4458  4459   0  2002   0.0             无记录   89.0    4.351     0.00      0.00   \n",
       "169    170   1  1998   0.0             无记录   89.0    6.203     0.00      0.00   \n",
       "3243  3244   1  1967   0.0             无记录  100.0    6.801     0.00      0.00   \n",
       "4488  4489   0  1951   0.0      父母有一方患有糖尿病   88.0    7.409     0.00      0.00   \n",
       "3062  3063   0  2003   0.0  叔叔或者姑姑有一方患有糖尿病   89.0    4.577     0.00      0.00   \n",
       "\n",
       "      患有糖尿病标识  体质指数-BMI  \n",
       "2135        1         4  \n",
       "1097        1         4  \n",
       "1013        1         4  \n",
       "479         1         4  \n",
       "1799        1         4  \n",
       "...       ...       ...  \n",
       "4458        0         0  \n",
       "169         0         0  \n",
       "3243        1         0  \n",
       "4488        0         0  \n",
       "3062        0         0  \n",
       "\n",
       "[5070 rows x 11 columns]"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 单列排序\n",
    "df.sort_values(by=\"体重指数\", ascending=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>编号</th>\n",
       "      <th>性别</th>\n",
       "      <th>出生年份</th>\n",
       "      <th>体重指数</th>\n",
       "      <th>糖尿病家族史</th>\n",
       "      <th>舒张压</th>\n",
       "      <th>口服耐糖量测试</th>\n",
       "      <th>胰岛素释放实验</th>\n",
       "      <th>肱三头肌皮褶厚度</th>\n",
       "      <th>患有糖尿病标识</th>\n",
       "      <th>体质指数-BMI</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>4581</th>\n",
       "      <td>4582</td>\n",
       "      <td>1</td>\n",
       "      <td>1943</td>\n",
       "      <td>24.7</td>\n",
       "      <td>叔叔或者姑姑有一方患有糖尿病</td>\n",
       "      <td>80.0</td>\n",
       "      <td>7.291</td>\n",
       "      <td>8.78</td>\n",
       "      <td>3.23</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1781</th>\n",
       "      <td>1782</td>\n",
       "      <td>0</td>\n",
       "      <td>1950</td>\n",
       "      <td>31.3</td>\n",
       "      <td>无记录</td>\n",
       "      <td>88.0</td>\n",
       "      <td>7.899</td>\n",
       "      <td>0.00</td>\n",
       "      <td>1.76</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4488</th>\n",
       "      <td>4489</td>\n",
       "      <td>0</td>\n",
       "      <td>1951</td>\n",
       "      <td>0.0</td>\n",
       "      <td>父母有一方患有糖尿病</td>\n",
       "      <td>88.0</td>\n",
       "      <td>7.409</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>984</th>\n",
       "      <td>985</td>\n",
       "      <td>0</td>\n",
       "      <td>1952</td>\n",
       "      <td>18.4</td>\n",
       "      <td>父母有一方患有糖尿病</td>\n",
       "      <td>89.0</td>\n",
       "      <td>6.478</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3398</th>\n",
       "      <td>3399</td>\n",
       "      <td>1</td>\n",
       "      <td>1952</td>\n",
       "      <td>34.4</td>\n",
       "      <td>父母有一方患有糖尿病</td>\n",
       "      <td>88.0</td>\n",
       "      <td>4.959</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1548</th>\n",
       "      <td>1549</td>\n",
       "      <td>1</td>\n",
       "      <td>2008</td>\n",
       "      <td>44.6</td>\n",
       "      <td>父母有一方患有糖尿病</td>\n",
       "      <td>95.0</td>\n",
       "      <td>7.102</td>\n",
       "      <td>7.23</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3743</th>\n",
       "      <td>3744</td>\n",
       "      <td>0</td>\n",
       "      <td>2008</td>\n",
       "      <td>47.9</td>\n",
       "      <td>无记录</td>\n",
       "      <td>88.0</td>\n",
       "      <td>8.964</td>\n",
       "      <td>8.76</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1039</th>\n",
       "      <td>1040</td>\n",
       "      <td>1</td>\n",
       "      <td>2009</td>\n",
       "      <td>25.1</td>\n",
       "      <td>无记录</td>\n",
       "      <td>87.0</td>\n",
       "      <td>6.431</td>\n",
       "      <td>0.00</td>\n",
       "      <td>41.70</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1178</th>\n",
       "      <td>1179</td>\n",
       "      <td>0</td>\n",
       "      <td>2009</td>\n",
       "      <td>25.5</td>\n",
       "      <td>无记录</td>\n",
       "      <td>93.0</td>\n",
       "      <td>6.869</td>\n",
       "      <td>8.24</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3538</th>\n",
       "      <td>3539</td>\n",
       "      <td>1</td>\n",
       "      <td>2009</td>\n",
       "      <td>31.7</td>\n",
       "      <td>无记录</td>\n",
       "      <td>83.0</td>\n",
       "      <td>4.946</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5070 rows × 11 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "        编号  性别  出生年份  体重指数          糖尿病家族史   舒张压  口服耐糖量测试  胰岛素释放实验  肱三头肌皮褶厚度  \\\n",
       "4581  4582   1  1943  24.7  叔叔或者姑姑有一方患有糖尿病  80.0    7.291     8.78      3.23   \n",
       "1781  1782   0  1950  31.3             无记录  88.0    7.899     0.00      1.76   \n",
       "4488  4489   0  1951   0.0      父母有一方患有糖尿病  88.0    7.409     0.00      0.00   \n",
       "984    985   0  1952  18.4      父母有一方患有糖尿病  89.0    6.478     0.00      0.00   \n",
       "3398  3399   1  1952  34.4      父母有一方患有糖尿病  88.0    4.959     0.00      0.00   \n",
       "...    ...  ..   ...   ...             ...   ...      ...      ...       ...   \n",
       "1548  1549   1  2008  44.6      父母有一方患有糖尿病  95.0    7.102     7.23      0.00   \n",
       "3743  3744   0  2008  47.9             无记录  88.0    8.964     8.76      0.00   \n",
       "1039  1040   1  2009  25.1             无记录  87.0    6.431     0.00     41.70   \n",
       "1178  1179   0  2009  25.5             无记录  93.0    6.869     8.24      0.00   \n",
       "3538  3539   1  2009  31.7             无记录  83.0    4.946     0.00      0.00   \n",
       "\n",
       "      患有糖尿病标识  体质指数-BMI  \n",
       "4581        0         2  \n",
       "1781        1         4  \n",
       "4488        0         0  \n",
       "984         0         0  \n",
       "3398        0         4  \n",
       "...       ...       ...  \n",
       "1548        0         4  \n",
       "3743        0         4  \n",
       "1039        1         2  \n",
       "1178        0         2  \n",
       "3538        0         4  \n",
       "\n",
       "[5070 rows x 11 columns]"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 多列排序\n",
    "df.sort_values(by=[ \"出生年份\",\"体重指数\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>编号</th>\n",
       "      <th>性别</th>\n",
       "      <th>出生年份</th>\n",
       "      <th>体重指数</th>\n",
       "      <th>糖尿病家族史</th>\n",
       "      <th>舒张压</th>\n",
       "      <th>口服耐糖量测试</th>\n",
       "      <th>胰岛素释放实验</th>\n",
       "      <th>肱三头肌皮褶厚度</th>\n",
       "      <th>患有糖尿病标识</th>\n",
       "      <th>体质指数-BMI</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>4581</th>\n",
       "      <td>4582</td>\n",
       "      <td>1</td>\n",
       "      <td>1943</td>\n",
       "      <td>24.7</td>\n",
       "      <td>叔叔或者姑姑有一方患有糖尿病</td>\n",
       "      <td>80.0</td>\n",
       "      <td>7.291</td>\n",
       "      <td>8.78</td>\n",
       "      <td>3.23</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1781</th>\n",
       "      <td>1782</td>\n",
       "      <td>0</td>\n",
       "      <td>1950</td>\n",
       "      <td>31.3</td>\n",
       "      <td>无记录</td>\n",
       "      <td>88.0</td>\n",
       "      <td>7.899</td>\n",
       "      <td>0.00</td>\n",
       "      <td>1.76</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4488</th>\n",
       "      <td>4489</td>\n",
       "      <td>0</td>\n",
       "      <td>1951</td>\n",
       "      <td>0.0</td>\n",
       "      <td>父母有一方患有糖尿病</td>\n",
       "      <td>88.0</td>\n",
       "      <td>7.409</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3398</th>\n",
       "      <td>3399</td>\n",
       "      <td>1</td>\n",
       "      <td>1952</td>\n",
       "      <td>34.4</td>\n",
       "      <td>父母有一方患有糖尿病</td>\n",
       "      <td>88.0</td>\n",
       "      <td>4.959</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>984</th>\n",
       "      <td>985</td>\n",
       "      <td>0</td>\n",
       "      <td>1952</td>\n",
       "      <td>18.4</td>\n",
       "      <td>父母有一方患有糖尿病</td>\n",
       "      <td>89.0</td>\n",
       "      <td>6.478</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1548</th>\n",
       "      <td>1549</td>\n",
       "      <td>1</td>\n",
       "      <td>2008</td>\n",
       "      <td>44.6</td>\n",
       "      <td>父母有一方患有糖尿病</td>\n",
       "      <td>95.0</td>\n",
       "      <td>7.102</td>\n",
       "      <td>7.23</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4886</th>\n",
       "      <td>4887</td>\n",
       "      <td>1</td>\n",
       "      <td>2008</td>\n",
       "      <td>31.3</td>\n",
       "      <td>无记录</td>\n",
       "      <td>89.0</td>\n",
       "      <td>3.587</td>\n",
       "      <td>36.37</td>\n",
       "      <td>44.40</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3538</th>\n",
       "      <td>3539</td>\n",
       "      <td>1</td>\n",
       "      <td>2009</td>\n",
       "      <td>31.7</td>\n",
       "      <td>无记录</td>\n",
       "      <td>83.0</td>\n",
       "      <td>4.946</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1178</th>\n",
       "      <td>1179</td>\n",
       "      <td>0</td>\n",
       "      <td>2009</td>\n",
       "      <td>25.5</td>\n",
       "      <td>无记录</td>\n",
       "      <td>93.0</td>\n",
       "      <td>6.869</td>\n",
       "      <td>8.24</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1039</th>\n",
       "      <td>1040</td>\n",
       "      <td>1</td>\n",
       "      <td>2009</td>\n",
       "      <td>25.1</td>\n",
       "      <td>无记录</td>\n",
       "      <td>87.0</td>\n",
       "      <td>6.431</td>\n",
       "      <td>0.00</td>\n",
       "      <td>41.70</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5070 rows × 11 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "        编号  性别  出生年份  体重指数          糖尿病家族史   舒张压  口服耐糖量测试  胰岛素释放实验  肱三头肌皮褶厚度  \\\n",
       "4581  4582   1  1943  24.7  叔叔或者姑姑有一方患有糖尿病  80.0    7.291     8.78      3.23   \n",
       "1781  1782   0  1950  31.3             无记录  88.0    7.899     0.00      1.76   \n",
       "4488  4489   0  1951   0.0      父母有一方患有糖尿病  88.0    7.409     0.00      0.00   \n",
       "3398  3399   1  1952  34.4      父母有一方患有糖尿病  88.0    4.959     0.00      0.00   \n",
       "984    985   0  1952  18.4      父母有一方患有糖尿病  89.0    6.478     0.00      0.00   \n",
       "...    ...  ..   ...   ...             ...   ...      ...      ...       ...   \n",
       "1548  1549   1  2008  44.6      父母有一方患有糖尿病  95.0    7.102     7.23      0.00   \n",
       "4886  4887   1  2008  31.3             无记录  89.0    3.587    36.37     44.40   \n",
       "3538  3539   1  2009  31.7             无记录  83.0    4.946     0.00      0.00   \n",
       "1178  1179   0  2009  25.5             无记录  93.0    6.869     8.24      0.00   \n",
       "1039  1040   1  2009  25.1             无记录  87.0    6.431     0.00     41.70   \n",
       "\n",
       "      患有糖尿病标识  体质指数-BMI  \n",
       "4581        0         2  \n",
       "1781        1         4  \n",
       "4488        0         0  \n",
       "3398        0         4  \n",
       "984         0         0  \n",
       "...       ...       ...  \n",
       "1548        0         4  \n",
       "4886        1         4  \n",
       "3538        0         4  \n",
       "1178        0         2  \n",
       "1039        1         2  \n",
       "\n",
       "[5070 rows x 11 columns]"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 混合排序\n",
    "df.sort_values(by=[ \"出生年份\",\"体重指数\"], ascending=[True, False])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 分组统计"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>性别</th>\n",
       "      <th>出生年份</th>\n",
       "      <th>体重指数</th>\n",
       "      <th>糖尿病家族史</th>\n",
       "      <th>舒张压</th>\n",
       "      <th>口服耐糖量测试</th>\n",
       "      <th>患有糖尿病标识</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>1996</td>\n",
       "      <td>30.1</td>\n",
       "      <td>无记录</td>\n",
       "      <td>106.0</td>\n",
       "      <td>3.818</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0</td>\n",
       "      <td>1988</td>\n",
       "      <td>27.5</td>\n",
       "      <td>无记录</td>\n",
       "      <td>84.0</td>\n",
       "      <td>-1.000</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>1988</td>\n",
       "      <td>36.5</td>\n",
       "      <td>无记录</td>\n",
       "      <td>85.0</td>\n",
       "      <td>7.131</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>1992</td>\n",
       "      <td>29.5</td>\n",
       "      <td>无记录</td>\n",
       "      <td>91.0</td>\n",
       "      <td>7.041</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0</td>\n",
       "      <td>1998</td>\n",
       "      <td>42.0</td>\n",
       "      <td>叔叔或者姑姑有一方患有糖尿病</td>\n",
       "      <td>NaN</td>\n",
       "      <td>7.134</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   性别  出生年份  体重指数          糖尿病家族史    舒张压  口服耐糖量测试  患有糖尿病标识\n",
       "0   0  1996  30.1             无记录  106.0    3.818        0\n",
       "1   0  1988  27.5             无记录   84.0   -1.000        0\n",
       "2   1  1988  36.5             无记录   85.0    7.131        1\n",
       "3   1  1992  29.5             无记录   91.0    7.041        0\n",
       "4   0  1998  42.0  叔叔或者姑姑有一方患有糖尿病    NaN    7.134        1"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.read_csv(r'./data/test_2022.csv', encoding = 'utf-8')\n",
    "df.drop([\"编号\",\"胰岛素释放实验\",\"肱三头肌皮褶厚度\"], axis=1, inplace=True)\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>出生年份</th>\n",
       "      <th>体重指数</th>\n",
       "      <th>舒张压</th>\n",
       "      <th>口服耐糖量测试</th>\n",
       "      <th>患有糖尿病标识</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>性别</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>5474531</td>\n",
       "      <td>102442.2</td>\n",
       "      <td>233071.0</td>\n",
       "      <td>15392.408</td>\n",
       "      <td>1013</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>4598896</td>\n",
       "      <td>90150.8</td>\n",
       "      <td>198219.0</td>\n",
       "      <td>13064.685</td>\n",
       "      <td>923</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       出生年份      体重指数       舒张压    口服耐糖量测试  患有糖尿病标识\n",
       "性别                                                 \n",
       "0   5474531  102442.2  233071.0  15392.408     1013\n",
       "1   4598896   90150.8  198219.0  13064.685      923"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.groupby(\"性别\").sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>出生年份</th>\n",
       "      <th>体重指数</th>\n",
       "      <th>舒张压</th>\n",
       "      <th>口服耐糖量测试</th>\n",
       "      <th>患有糖尿病标识</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>性别</th>\n",
       "      <th>糖尿病家族史</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"4\" valign=\"top\">0</th>\n",
       "      <th>叔叔或姑姑有一方患有糖尿病</th>\n",
       "      <td>1986.010909</td>\n",
       "      <td>39.687455</td>\n",
       "      <td>91.534840</td>\n",
       "      <td>5.604165</td>\n",
       "      <td>0.405455</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>叔叔或者姑姑有一方患有糖尿病</th>\n",
       "      <td>1994.496774</td>\n",
       "      <td>30.020000</td>\n",
       "      <td>72.979730</td>\n",
       "      <td>5.930129</td>\n",
       "      <td>0.258065</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>无记录</th>\n",
       "      <td>1987.377622</td>\n",
       "      <td>37.593388</td>\n",
       "      <td>90.461383</td>\n",
       "      <td>5.504991</td>\n",
       "      <td>0.363636</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>父母有一方患有糖尿病</th>\n",
       "      <td>1989.355042</td>\n",
       "      <td>35.350000</td>\n",
       "      <td>85.082969</td>\n",
       "      <td>5.738647</td>\n",
       "      <td>0.373950</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"4\" valign=\"top\">1</th>\n",
       "      <th>叔叔或姑姑有一方患有糖尿病</th>\n",
       "      <td>1986.305243</td>\n",
       "      <td>40.173221</td>\n",
       "      <td>91.285433</td>\n",
       "      <td>5.508399</td>\n",
       "      <td>0.383895</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>叔叔或者姑姑有一方患有糖尿病</th>\n",
       "      <td>1981.440678</td>\n",
       "      <td>31.732203</td>\n",
       "      <td>76.714286</td>\n",
       "      <td>6.516322</td>\n",
       "      <td>0.457627</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>无记录</th>\n",
       "      <td>1985.765106</td>\n",
       "      <td>38.965937</td>\n",
       "      <td>90.800794</td>\n",
       "      <td>5.585471</td>\n",
       "      <td>0.399547</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>父母有一方患有糖尿病</th>\n",
       "      <td>1985.340852</td>\n",
       "      <td>38.183459</td>\n",
       "      <td>88.849866</td>\n",
       "      <td>5.873619</td>\n",
       "      <td>0.406015</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                          出生年份       体重指数        舒张压   口服耐糖量测试   患有糖尿病标识\n",
       "性别 糖尿病家族史                                                               \n",
       "0  叔叔或姑姑有一方患有糖尿病   1986.010909  39.687455  91.534840  5.604165  0.405455\n",
       "   叔叔或者姑姑有一方患有糖尿病  1994.496774  30.020000  72.979730  5.930129  0.258065\n",
       "   无记录             1987.377622  37.593388  90.461383  5.504991  0.363636\n",
       "   父母有一方患有糖尿病      1989.355042  35.350000  85.082969  5.738647  0.373950\n",
       "1  叔叔或姑姑有一方患有糖尿病   1986.305243  40.173221  91.285433  5.508399  0.383895\n",
       "   叔叔或者姑姑有一方患有糖尿病  1981.440678  31.732203  76.714286  6.516322  0.457627\n",
       "   无记录             1985.765106  38.965937  90.800794  5.585471  0.399547\n",
       "   父母有一方患有糖尿病      1985.340852  38.183459  88.849866  5.873619  0.406015"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.groupby([\"性别\",\"糖尿病家族史\"]).mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>性别</th>\n",
       "      <th>糖尿病家族史</th>\n",
       "      <th>出生年份</th>\n",
       "      <th>体重指数</th>\n",
       "      <th>舒张压</th>\n",
       "      <th>口服耐糖量测试</th>\n",
       "      <th>患有糖尿病标识</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>叔叔或姑姑有一方患有糖尿病</td>\n",
       "      <td>1986.010909</td>\n",
       "      <td>39.687455</td>\n",
       "      <td>91.534840</td>\n",
       "      <td>5.604165</td>\n",
       "      <td>0.405455</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0</td>\n",
       "      <td>叔叔或者姑姑有一方患有糖尿病</td>\n",
       "      <td>1994.496774</td>\n",
       "      <td>30.020000</td>\n",
       "      <td>72.979730</td>\n",
       "      <td>5.930129</td>\n",
       "      <td>0.258065</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0</td>\n",
       "      <td>无记录</td>\n",
       "      <td>1987.377622</td>\n",
       "      <td>37.593388</td>\n",
       "      <td>90.461383</td>\n",
       "      <td>5.504991</td>\n",
       "      <td>0.363636</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0</td>\n",
       "      <td>父母有一方患有糖尿病</td>\n",
       "      <td>1989.355042</td>\n",
       "      <td>35.350000</td>\n",
       "      <td>85.082969</td>\n",
       "      <td>5.738647</td>\n",
       "      <td>0.373950</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1</td>\n",
       "      <td>叔叔或姑姑有一方患有糖尿病</td>\n",
       "      <td>1986.305243</td>\n",
       "      <td>40.173221</td>\n",
       "      <td>91.285433</td>\n",
       "      <td>5.508399</td>\n",
       "      <td>0.383895</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>1</td>\n",
       "      <td>叔叔或者姑姑有一方患有糖尿病</td>\n",
       "      <td>1981.440678</td>\n",
       "      <td>31.732203</td>\n",
       "      <td>76.714286</td>\n",
       "      <td>6.516322</td>\n",
       "      <td>0.457627</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>1</td>\n",
       "      <td>无记录</td>\n",
       "      <td>1985.765106</td>\n",
       "      <td>38.965937</td>\n",
       "      <td>90.800794</td>\n",
       "      <td>5.585471</td>\n",
       "      <td>0.399547</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>1</td>\n",
       "      <td>父母有一方患有糖尿病</td>\n",
       "      <td>1985.340852</td>\n",
       "      <td>38.183459</td>\n",
       "      <td>88.849866</td>\n",
       "      <td>5.873619</td>\n",
       "      <td>0.406015</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   性别          糖尿病家族史         出生年份       体重指数        舒张压   口服耐糖量测试   患有糖尿病标识\n",
       "0   0   叔叔或姑姑有一方患有糖尿病  1986.010909  39.687455  91.534840  5.604165  0.405455\n",
       "1   0  叔叔或者姑姑有一方患有糖尿病  1994.496774  30.020000  72.979730  5.930129  0.258065\n",
       "2   0             无记录  1987.377622  37.593388  90.461383  5.504991  0.363636\n",
       "3   0      父母有一方患有糖尿病  1989.355042  35.350000  85.082969  5.738647  0.373950\n",
       "4   1   叔叔或姑姑有一方患有糖尿病  1986.305243  40.173221  91.285433  5.508399  0.383895\n",
       "5   1  叔叔或者姑姑有一方患有糖尿病  1981.440678  31.732203  76.714286  6.516322  0.457627\n",
       "6   1             无记录  1985.765106  38.965937  90.800794  5.585471  0.399547\n",
       "7   1      父母有一方患有糖尿病  1985.340852  38.183459  88.849866  5.873619  0.406015"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.groupby([\"性别\",\"糖尿病家族史\"], as_index=False).mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>出生年份</th>\n",
       "      <th>体重指数</th>\n",
       "      <th>舒张压</th>\n",
       "      <th>口服耐糖量测试</th>\n",
       "      <th>患有糖尿病标识</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>性别</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>5474531</td>\n",
       "      <td>102442.2</td>\n",
       "      <td>233071.0</td>\n",
       "      <td>15392.408</td>\n",
       "      <td>1013</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>4598896</td>\n",
       "      <td>90150.8</td>\n",
       "      <td>198219.0</td>\n",
       "      <td>13064.685</td>\n",
       "      <td>923</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       出生年份      体重指数       舒张压    口服耐糖量测试  患有糖尿病标识\n",
       "性别                                                 \n",
       "0   5474531  102442.2  233071.0  15392.408     1013\n",
       "1   4598896   90150.8  198219.0  13064.685      923"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.groupby(\"性别\").sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr:last-of-type th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th colspan=\"2\" halign=\"left\">出生年份</th>\n",
       "      <th colspan=\"2\" halign=\"left\">体重指数</th>\n",
       "      <th colspan=\"2\" halign=\"left\">舒张压</th>\n",
       "      <th colspan=\"2\" halign=\"left\">口服耐糖量测试</th>\n",
       "      <th colspan=\"2\" halign=\"left\">患有糖尿病标识</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th>sum</th>\n",
       "      <th>std</th>\n",
       "      <th>sum</th>\n",
       "      <th>std</th>\n",
       "      <th>sum</th>\n",
       "      <th>std</th>\n",
       "      <th>sum</th>\n",
       "      <th>std</th>\n",
       "      <th>sum</th>\n",
       "      <th>std</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>性别</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>5474531</td>\n",
       "      <td>9.063337</td>\n",
       "      <td>102442.2</td>\n",
       "      <td>11.430054</td>\n",
       "      <td>233071.0</td>\n",
       "      <td>10.437178</td>\n",
       "      <td>15392.408</td>\n",
       "      <td>2.300444</td>\n",
       "      <td>1013</td>\n",
       "      <td>0.482302</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>4598896</td>\n",
       "      <td>8.604342</td>\n",
       "      <td>90150.8</td>\n",
       "      <td>11.398870</td>\n",
       "      <td>198219.0</td>\n",
       "      <td>7.561833</td>\n",
       "      <td>13064.685</td>\n",
       "      <td>2.205843</td>\n",
       "      <td>923</td>\n",
       "      <td>0.489702</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       出生年份                体重指数                  舒张压               口服耐糖量测试  \\\n",
       "        sum       std       sum        std       sum        std        sum   \n",
       "性别                                                                           \n",
       "0   5474531  9.063337  102442.2  11.430054  233071.0  10.437178  15392.408   \n",
       "1   4598896  8.604342   90150.8  11.398870  198219.0   7.561833  13064.685   \n",
       "\n",
       "             患有糖尿病标识            \n",
       "         std     sum       std  \n",
       "性别                              \n",
       "0   2.300444    1013  0.482302  \n",
       "1   2.205843     923  0.489702  "
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.groupby(\"性别\").agg([np.sum, np.std])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>舒张压</th>\n",
       "      <th>体重指数</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>性别</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>233071.0</td>\n",
       "      <td>11.430054</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>198219.0</td>\n",
       "      <td>11.398870</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         舒张压       体重指数\n",
       "性别                     \n",
       "0   233071.0  11.430054\n",
       "1   198219.0  11.398870"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.groupby(\"性别\").agg({\"舒张压\":np.sum, \"体重指数\":np.std})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>sum</th>\n",
       "      <th>std</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>性别</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>102442.2</td>\n",
       "      <td>11.430054</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>90150.8</td>\n",
       "      <td>11.398870</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         sum        std\n",
       "性别                     \n",
       "0   102442.2  11.430054\n",
       "1    90150.8  11.398870"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#统计单列\n",
    "#方法一（先过滤，性能更快）：\n",
    "df.groupby(\"性别\")[\"体重指数\"].agg([np.sum, np.std])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#方法二："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>sum</th>\n",
       "      <th>std</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>性别</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>102442.2</td>\n",
       "      <td>11.430054</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>90150.8</td>\n",
       "      <td>11.398870</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         sum        std\n",
       "性别                     \n",
       "0   102442.2  11.430054\n",
       "1    90150.8  11.398870"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.groupby(\"性别\").agg([np.sum, np.std])[\"体重指数\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>舒张压</th>\n",
       "      <th>体重指数</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>性别</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>233071.0</td>\n",
       "      <td>11.430054</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>198219.0</td>\n",
       "      <td>11.398870</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         舒张压       体重指数\n",
       "性别                     \n",
       "0   233071.0  11.430054\n",
       "1   198219.0  11.398870"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 多级索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "性别  糖尿病家族史        \n",
       "0   叔叔或姑姑有一方患有糖尿病     0.405455\n",
       "    叔叔或者姑姑有一方患有糖尿病    0.258065\n",
       "    无记录               0.363636\n",
       "    父母有一方患有糖尿病        0.373950\n",
       "1   叔叔或姑姑有一方患有糖尿病     0.383895\n",
       "    叔叔或者姑姑有一方患有糖尿病    0.457627\n",
       "    无记录               0.399547\n",
       "    父母有一方患有糖尿病        0.406015\n",
       "Name: 患有糖尿病标识, dtype: float64"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ser = df.groupby([\"性别\",\"糖尿病家族史\"])[\"患有糖尿病标识\"].mean()\n",
    "ser"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "MultiIndex([(0,  '叔叔或姑姑有一方患有糖尿病'),\n",
       "            (0, '叔叔或者姑姑有一方患有糖尿病'),\n",
       "            (0,            '无记录'),\n",
       "            (0,     '父母有一方患有糖尿病'),\n",
       "            (1,  '叔叔或姑姑有一方患有糖尿病'),\n",
       "            (1, '叔叔或者姑姑有一方患有糖尿病'),\n",
       "            (1,            '无记录'),\n",
       "            (1,     '父母有一方患有糖尿病')],\n",
       "           names=['性别', '糖尿病家族史'])"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ser.index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "糖尿病家族史          性别\n",
       "叔叔或姑姑有一方患有糖尿病   0     0.405455\n",
       "叔叔或者姑姑有一方患有糖尿病  0     0.258065\n",
       "无记录             0     0.363636\n",
       "父母有一方患有糖尿病      0     0.373950\n",
       "叔叔或姑姑有一方患有糖尿病   1     0.383895\n",
       "叔叔或者姑姑有一方患有糖尿病  1     0.457627\n",
       "无记录             1     0.399547\n",
       "父母有一方患有糖尿病      1     0.406015\n",
       "Name: 患有糖尿病标识, dtype: float64"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ser.swaplevel()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>糖尿病家族史</th>\n",
       "      <th>叔叔或姑姑有一方患有糖尿病</th>\n",
       "      <th>叔叔或者姑姑有一方患有糖尿病</th>\n",
       "      <th>无记录</th>\n",
       "      <th>父母有一方患有糖尿病</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>性别</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.405455</td>\n",
       "      <td>0.258065</td>\n",
       "      <td>0.363636</td>\n",
       "      <td>0.373950</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.383895</td>\n",
       "      <td>0.457627</td>\n",
       "      <td>0.399547</td>\n",
       "      <td>0.406015</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "糖尿病家族史  叔叔或姑姑有一方患有糖尿病  叔叔或者姑姑有一方患有糖尿病       无记录  父母有一方患有糖尿病\n",
       "性别                                                         \n",
       "0            0.405455        0.258065  0.363636    0.373950\n",
       "1            0.383895        0.457627  0.399547    0.406015"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ser.unstack()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>性别</th>\n",
       "      <th>糖尿病家族史</th>\n",
       "      <th>患有糖尿病标识</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>叔叔或姑姑有一方患有糖尿病</td>\n",
       "      <td>0.405455</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0</td>\n",
       "      <td>叔叔或者姑姑有一方患有糖尿病</td>\n",
       "      <td>0.258065</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0</td>\n",
       "      <td>无记录</td>\n",
       "      <td>0.363636</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0</td>\n",
       "      <td>父母有一方患有糖尿病</td>\n",
       "      <td>0.373950</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1</td>\n",
       "      <td>叔叔或姑姑有一方患有糖尿病</td>\n",
       "      <td>0.383895</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>1</td>\n",
       "      <td>叔叔或者姑姑有一方患有糖尿病</td>\n",
       "      <td>0.457627</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>1</td>\n",
       "      <td>无记录</td>\n",
       "      <td>0.399547</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>1</td>\n",
       "      <td>父母有一方患有糖尿病</td>\n",
       "      <td>0.406015</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   性别          糖尿病家族史   患有糖尿病标识\n",
       "0   0   叔叔或姑姑有一方患有糖尿病  0.405455\n",
       "1   0  叔叔或者姑姑有一方患有糖尿病  0.258065\n",
       "2   0             无记录  0.363636\n",
       "3   0      父母有一方患有糖尿病  0.373950\n",
       "4   1   叔叔或姑姑有一方患有糖尿病  0.383895\n",
       "5   1  叔叔或者姑姑有一方患有糖尿病  0.457627\n",
       "6   1             无记录  0.399547\n",
       "7   1      父母有一方患有糖尿病  0.406015"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ser.reset_index()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>出生年份</th>\n",
       "      <th>体重指数</th>\n",
       "      <th>舒张压</th>\n",
       "      <th>口服耐糖量测试</th>\n",
       "      <th>患有糖尿病标识</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>性别</th>\n",
       "      <th>糖尿病家族史</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"4\" valign=\"top\">0</th>\n",
       "      <th>叔叔或姑姑有一方患有糖尿病</th>\n",
       "      <td>1986.010909</td>\n",
       "      <td>39.687455</td>\n",
       "      <td>91.534840</td>\n",
       "      <td>5.604165</td>\n",
       "      <td>0.405455</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>叔叔或者姑姑有一方患有糖尿病</th>\n",
       "      <td>1994.496774</td>\n",
       "      <td>30.020000</td>\n",
       "      <td>72.979730</td>\n",
       "      <td>5.930129</td>\n",
       "      <td>0.258065</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>无记录</th>\n",
       "      <td>1987.377622</td>\n",
       "      <td>37.593388</td>\n",
       "      <td>90.461383</td>\n",
       "      <td>5.504991</td>\n",
       "      <td>0.363636</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>父母有一方患有糖尿病</th>\n",
       "      <td>1989.355042</td>\n",
       "      <td>35.350000</td>\n",
       "      <td>85.082969</td>\n",
       "      <td>5.738647</td>\n",
       "      <td>0.373950</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"4\" valign=\"top\">1</th>\n",
       "      <th>叔叔或姑姑有一方患有糖尿病</th>\n",
       "      <td>1986.305243</td>\n",
       "      <td>40.173221</td>\n",
       "      <td>91.285433</td>\n",
       "      <td>5.508399</td>\n",
       "      <td>0.383895</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>叔叔或者姑姑有一方患有糖尿病</th>\n",
       "      <td>1981.440678</td>\n",
       "      <td>31.732203</td>\n",
       "      <td>76.714286</td>\n",
       "      <td>6.516322</td>\n",
       "      <td>0.457627</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>无记录</th>\n",
       "      <td>1985.765106</td>\n",
       "      <td>38.965937</td>\n",
       "      <td>90.800794</td>\n",
       "      <td>5.585471</td>\n",
       "      <td>0.399547</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>父母有一方患有糖尿病</th>\n",
       "      <td>1985.340852</td>\n",
       "      <td>38.183459</td>\n",
       "      <td>88.849866</td>\n",
       "      <td>5.873619</td>\n",
       "      <td>0.406015</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                          出生年份       体重指数        舒张压   口服耐糖量测试   患有糖尿病标识\n",
       "性别 糖尿病家族史                                                               \n",
       "0  叔叔或姑姑有一方患有糖尿病   1986.010909  39.687455  91.534840  5.604165  0.405455\n",
       "   叔叔或者姑姑有一方患有糖尿病  1994.496774  30.020000  72.979730  5.930129  0.258065\n",
       "   无记录             1987.377622  37.593388  90.461383  5.504991  0.363636\n",
       "   父母有一方患有糖尿病      1989.355042  35.350000  85.082969  5.738647  0.373950\n",
       "1  叔叔或姑姑有一方患有糖尿病   1986.305243  40.173221  91.285433  5.508399  0.383895\n",
       "   叔叔或者姑姑有一方患有糖尿病  1981.440678  31.732203  76.714286  6.516322  0.457627\n",
       "   无记录             1985.765106  38.965937  90.800794  5.585471  0.399547\n",
       "   父母有一方患有糖尿病      1985.340852  38.183459  88.849866  5.873619  0.406015"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2 = df.groupby([\"性别\",\"糖尿病家族史\"]).mean()\n",
    "df2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "MultiIndex([(0,  '叔叔或姑姑有一方患有糖尿病'),\n",
       "            (0, '叔叔或者姑姑有一方患有糖尿病'),\n",
       "            (0,            '无记录'),\n",
       "            (0,     '父母有一方患有糖尿病'),\n",
       "            (1,  '叔叔或姑姑有一方患有糖尿病'),\n",
       "            (1, '叔叔或者姑姑有一方患有糖尿病'),\n",
       "            (1,            '无记录'),\n",
       "            (1,     '父母有一方患有糖尿病')],\n",
       "           names=['性别', '糖尿病家族史'])"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2.index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>出生年份</th>\n",
       "      <th>体重指数</th>\n",
       "      <th>舒张压</th>\n",
       "      <th>口服耐糖量测试</th>\n",
       "      <th>患有糖尿病标识</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>糖尿病家族史</th>\n",
       "      <th>性别</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>叔叔或姑姑有一方患有糖尿病</th>\n",
       "      <th>0</th>\n",
       "      <td>1986.010909</td>\n",
       "      <td>39.687455</td>\n",
       "      <td>91.534840</td>\n",
       "      <td>5.604165</td>\n",
       "      <td>0.405455</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>叔叔或者姑姑有一方患有糖尿病</th>\n",
       "      <th>0</th>\n",
       "      <td>1994.496774</td>\n",
       "      <td>30.020000</td>\n",
       "      <td>72.979730</td>\n",
       "      <td>5.930129</td>\n",
       "      <td>0.258065</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>无记录</th>\n",
       "      <th>0</th>\n",
       "      <td>1987.377622</td>\n",
       "      <td>37.593388</td>\n",
       "      <td>90.461383</td>\n",
       "      <td>5.504991</td>\n",
       "      <td>0.363636</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>父母有一方患有糖尿病</th>\n",
       "      <th>0</th>\n",
       "      <td>1989.355042</td>\n",
       "      <td>35.350000</td>\n",
       "      <td>85.082969</td>\n",
       "      <td>5.738647</td>\n",
       "      <td>0.373950</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>叔叔或姑姑有一方患有糖尿病</th>\n",
       "      <th>1</th>\n",
       "      <td>1986.305243</td>\n",
       "      <td>40.173221</td>\n",
       "      <td>91.285433</td>\n",
       "      <td>5.508399</td>\n",
       "      <td>0.383895</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>叔叔或者姑姑有一方患有糖尿病</th>\n",
       "      <th>1</th>\n",
       "      <td>1981.440678</td>\n",
       "      <td>31.732203</td>\n",
       "      <td>76.714286</td>\n",
       "      <td>6.516322</td>\n",
       "      <td>0.457627</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>无记录</th>\n",
       "      <th>1</th>\n",
       "      <td>1985.765106</td>\n",
       "      <td>38.965937</td>\n",
       "      <td>90.800794</td>\n",
       "      <td>5.585471</td>\n",
       "      <td>0.399547</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>父母有一方患有糖尿病</th>\n",
       "      <th>1</th>\n",
       "      <td>1985.340852</td>\n",
       "      <td>38.183459</td>\n",
       "      <td>88.849866</td>\n",
       "      <td>5.873619</td>\n",
       "      <td>0.406015</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                          出生年份       体重指数        舒张压   口服耐糖量测试   患有糖尿病标识\n",
       "糖尿病家族史         性别                                                       \n",
       "叔叔或姑姑有一方患有糖尿病  0   1986.010909  39.687455  91.534840  5.604165  0.405455\n",
       "叔叔或者姑姑有一方患有糖尿病 0   1994.496774  30.020000  72.979730  5.930129  0.258065\n",
       "无记录            0   1987.377622  37.593388  90.461383  5.504991  0.363636\n",
       "父母有一方患有糖尿病     0   1989.355042  35.350000  85.082969  5.738647  0.373950\n",
       "叔叔或姑姑有一方患有糖尿病  1   1986.305243  40.173221  91.285433  5.508399  0.383895\n",
       "叔叔或者姑姑有一方患有糖尿病 1   1981.440678  31.732203  76.714286  6.516322  0.457627\n",
       "无记录            1   1985.765106  38.965937  90.800794  5.585471  0.399547\n",
       "父母有一方患有糖尿病     1   1985.340852  38.183459  88.849866  5.873619  0.406015"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2.swaplevel()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr:last-of-type th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th colspan=\"4\" halign=\"left\">出生年份</th>\n",
       "      <th colspan=\"4\" halign=\"left\">体重指数</th>\n",
       "      <th colspan=\"4\" halign=\"left\">舒张压</th>\n",
       "      <th colspan=\"4\" halign=\"left\">口服耐糖量测试</th>\n",
       "      <th colspan=\"4\" halign=\"left\">患有糖尿病标识</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>糖尿病家族史</th>\n",
       "      <th>叔叔或姑姑有一方患有糖尿病</th>\n",
       "      <th>叔叔或者姑姑有一方患有糖尿病</th>\n",
       "      <th>无记录</th>\n",
       "      <th>父母有一方患有糖尿病</th>\n",
       "      <th>叔叔或姑姑有一方患有糖尿病</th>\n",
       "      <th>叔叔或者姑姑有一方患有糖尿病</th>\n",
       "      <th>无记录</th>\n",
       "      <th>父母有一方患有糖尿病</th>\n",
       "      <th>叔叔或姑姑有一方患有糖尿病</th>\n",
       "      <th>叔叔或者姑姑有一方患有糖尿病</th>\n",
       "      <th>无记录</th>\n",
       "      <th>父母有一方患有糖尿病</th>\n",
       "      <th>叔叔或姑姑有一方患有糖尿病</th>\n",
       "      <th>叔叔或者姑姑有一方患有糖尿病</th>\n",
       "      <th>无记录</th>\n",
       "      <th>父母有一方患有糖尿病</th>\n",
       "      <th>叔叔或姑姑有一方患有糖尿病</th>\n",
       "      <th>叔叔或者姑姑有一方患有糖尿病</th>\n",
       "      <th>无记录</th>\n",
       "      <th>父母有一方患有糖尿病</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>性别</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1986.010909</td>\n",
       "      <td>1994.496774</td>\n",
       "      <td>1987.377622</td>\n",
       "      <td>1989.355042</td>\n",
       "      <td>39.687455</td>\n",
       "      <td>30.020000</td>\n",
       "      <td>37.593388</td>\n",
       "      <td>35.350000</td>\n",
       "      <td>91.534840</td>\n",
       "      <td>72.979730</td>\n",
       "      <td>90.461383</td>\n",
       "      <td>85.082969</td>\n",
       "      <td>5.604165</td>\n",
       "      <td>5.930129</td>\n",
       "      <td>5.504991</td>\n",
       "      <td>5.738647</td>\n",
       "      <td>0.405455</td>\n",
       "      <td>0.258065</td>\n",
       "      <td>0.363636</td>\n",
       "      <td>0.373950</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1986.305243</td>\n",
       "      <td>1981.440678</td>\n",
       "      <td>1985.765106</td>\n",
       "      <td>1985.340852</td>\n",
       "      <td>40.173221</td>\n",
       "      <td>31.732203</td>\n",
       "      <td>38.965937</td>\n",
       "      <td>38.183459</td>\n",
       "      <td>91.285433</td>\n",
       "      <td>76.714286</td>\n",
       "      <td>90.800794</td>\n",
       "      <td>88.849866</td>\n",
       "      <td>5.508399</td>\n",
       "      <td>6.516322</td>\n",
       "      <td>5.585471</td>\n",
       "      <td>5.873619</td>\n",
       "      <td>0.383895</td>\n",
       "      <td>0.457627</td>\n",
       "      <td>0.399547</td>\n",
       "      <td>0.406015</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                出生年份                                                   体重指数  \\\n",
       "糖尿病家族史 叔叔或姑姑有一方患有糖尿病 叔叔或者姑姑有一方患有糖尿病          无记录   父母有一方患有糖尿病 叔叔或姑姑有一方患有糖尿病   \n",
       "性别                                                                            \n",
       "0        1986.010909    1994.496774  1987.377622  1989.355042     39.687455   \n",
       "1        1986.305243    1981.440678  1985.765106  1985.340852     40.173221   \n",
       "\n",
       "                                                      舒张压                 \\\n",
       "糖尿病家族史 叔叔或者姑姑有一方患有糖尿病        无记录 父母有一方患有糖尿病 叔叔或姑姑有一方患有糖尿病 叔叔或者姑姑有一方患有糖尿病   \n",
       "性别                                                                         \n",
       "0           30.020000  37.593388  35.350000     91.534840      72.979730   \n",
       "1           31.732203  38.965937  38.183459     91.285433      76.714286   \n",
       "\n",
       "                                   口服耐糖量测试                           \\\n",
       "糖尿病家族史        无记录 父母有一方患有糖尿病 叔叔或姑姑有一方患有糖尿病 叔叔或者姑姑有一方患有糖尿病       无记录   \n",
       "性别                                                                    \n",
       "0       90.461383  85.082969      5.604165       5.930129  5.504991   \n",
       "1       90.800794  88.849866      5.508399       6.516322  5.585471   \n",
       "\n",
       "                        患有糖尿病标识                                      \n",
       "糖尿病家族史 父母有一方患有糖尿病 叔叔或姑姑有一方患有糖尿病 叔叔或者姑姑有一方患有糖尿病       无记录 父母有一方患有糖尿病  \n",
       "性别                                                                   \n",
       "0        5.738647      0.405455       0.258065  0.363636   0.373950  \n",
       "1        5.873619      0.383895       0.457627  0.399547   0.406015  "
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2.unstack()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>性别</th>\n",
       "      <th>糖尿病家族史</th>\n",
       "      <th>出生年份</th>\n",
       "      <th>体重指数</th>\n",
       "      <th>舒张压</th>\n",
       "      <th>口服耐糖量测试</th>\n",
       "      <th>患有糖尿病标识</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>叔叔或姑姑有一方患有糖尿病</td>\n",
       "      <td>1986.010909</td>\n",
       "      <td>39.687455</td>\n",
       "      <td>91.534840</td>\n",
       "      <td>5.604165</td>\n",
       "      <td>0.405455</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0</td>\n",
       "      <td>叔叔或者姑姑有一方患有糖尿病</td>\n",
       "      <td>1994.496774</td>\n",
       "      <td>30.020000</td>\n",
       "      <td>72.979730</td>\n",
       "      <td>5.930129</td>\n",
       "      <td>0.258065</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0</td>\n",
       "      <td>无记录</td>\n",
       "      <td>1987.377622</td>\n",
       "      <td>37.593388</td>\n",
       "      <td>90.461383</td>\n",
       "      <td>5.504991</td>\n",
       "      <td>0.363636</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0</td>\n",
       "      <td>父母有一方患有糖尿病</td>\n",
       "      <td>1989.355042</td>\n",
       "      <td>35.350000</td>\n",
       "      <td>85.082969</td>\n",
       "      <td>5.738647</td>\n",
       "      <td>0.373950</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1</td>\n",
       "      <td>叔叔或姑姑有一方患有糖尿病</td>\n",
       "      <td>1986.305243</td>\n",
       "      <td>40.173221</td>\n",
       "      <td>91.285433</td>\n",
       "      <td>5.508399</td>\n",
       "      <td>0.383895</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>1</td>\n",
       "      <td>叔叔或者姑姑有一方患有糖尿病</td>\n",
       "      <td>1981.440678</td>\n",
       "      <td>31.732203</td>\n",
       "      <td>76.714286</td>\n",
       "      <td>6.516322</td>\n",
       "      <td>0.457627</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>1</td>\n",
       "      <td>无记录</td>\n",
       "      <td>1985.765106</td>\n",
       "      <td>38.965937</td>\n",
       "      <td>90.800794</td>\n",
       "      <td>5.585471</td>\n",
       "      <td>0.399547</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>1</td>\n",
       "      <td>父母有一方患有糖尿病</td>\n",
       "      <td>1985.340852</td>\n",
       "      <td>38.183459</td>\n",
       "      <td>88.849866</td>\n",
       "      <td>5.873619</td>\n",
       "      <td>0.406015</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   性别          糖尿病家族史         出生年份       体重指数        舒张压   口服耐糖量测试   患有糖尿病标识\n",
       "0   0   叔叔或姑姑有一方患有糖尿病  1986.010909  39.687455  91.534840  5.604165  0.405455\n",
       "1   0  叔叔或者姑姑有一方患有糖尿病  1994.496774  30.020000  72.979730  5.930129  0.258065\n",
       "2   0             无记录  1987.377622  37.593388  90.461383  5.504991  0.363636\n",
       "3   0      父母有一方患有糖尿病  1989.355042  35.350000  85.082969  5.738647  0.373950\n",
       "4   1   叔叔或姑姑有一方患有糖尿病  1986.305243  40.173221  91.285433  5.508399  0.383895\n",
       "5   1  叔叔或者姑姑有一方患有糖尿病  1981.440678  31.732203  76.714286  6.516322  0.457627\n",
       "6   1             无记录  1985.765106  38.965937  90.800794  5.585471  0.399547\n",
       "7   1      父母有一方患有糖尿病  1985.340852  38.183459  88.849866  5.873619  0.406015"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2.reset_index()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## stack和pivot实现数据透视"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>性别</th>\n",
       "      <th>出生年份</th>\n",
       "      <th>体重指数</th>\n",
       "      <th>糖尿病家族史</th>\n",
       "      <th>舒张压</th>\n",
       "      <th>口服耐糖量测试</th>\n",
       "      <th>患有糖尿病标识</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>1996</td>\n",
       "      <td>30.1</td>\n",
       "      <td>无记录</td>\n",
       "      <td>106.0</td>\n",
       "      <td>3.818</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0</td>\n",
       "      <td>1988</td>\n",
       "      <td>27.5</td>\n",
       "      <td>无记录</td>\n",
       "      <td>84.0</td>\n",
       "      <td>-1.000</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>1988</td>\n",
       "      <td>36.5</td>\n",
       "      <td>无记录</td>\n",
       "      <td>85.0</td>\n",
       "      <td>7.131</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>1992</td>\n",
       "      <td>29.5</td>\n",
       "      <td>无记录</td>\n",
       "      <td>91.0</td>\n",
       "      <td>7.041</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0</td>\n",
       "      <td>1998</td>\n",
       "      <td>42.0</td>\n",
       "      <td>叔叔或者姑姑有一方患有糖尿病</td>\n",
       "      <td>NaN</td>\n",
       "      <td>7.134</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   性别  出生年份  体重指数          糖尿病家族史    舒张压  口服耐糖量测试  患有糖尿病标识\n",
       "0   0  1996  30.1             无记录  106.0    3.818        0\n",
       "1   0  1988  27.5             无记录   84.0   -1.000        0\n",
       "2   1  1988  36.5             无记录   85.0    7.131        1\n",
       "3   1  1992  29.5             无记录   91.0    7.041        0\n",
       "4   0  1998  42.0  叔叔或者姑姑有一方患有糖尿病    NaN    7.134        1"
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>weights</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>性别</th>\n",
       "      <th>患有糖尿病标识</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">0</th>\n",
       "      <th>0</th>\n",
       "      <td>33.838771</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>42.970286</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">1</th>\n",
       "      <th>0</th>\n",
       "      <td>35.522111</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>44.061213</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              weights\n",
       "性别 患有糖尿病标识           \n",
       "0  0        33.838771\n",
       "   1        42.970286\n",
       "1  0        35.522111\n",
       "   1        44.061213"
      ]
     },
     "execution_count": 76,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df3 = df.groupby([\"性别\",\"患有糖尿病标识\"])[\"体重指数\"].agg(weights=np.mean)\n",
    "df3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr:last-of-type th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th colspan=\"2\" halign=\"left\">weights</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>患有糖尿病标识</th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>性别</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>33.838771</td>\n",
       "      <td>42.970286</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>35.522111</td>\n",
       "      <td>44.061213</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "           weights           \n",
       "患有糖尿病标识          0          1\n",
       "性别                           \n",
       "0        33.838771  42.970286\n",
       "1        35.522111  44.061213"
      ]
     },
     "execution_count": 78,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_stack = df3.unstack()\n",
    "df_stack"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.axes._subplots.AxesSubplot at 0x24d71e77b70>"
      ]
     },
     "execution_count": 79,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXQAAAEKCAYAAAACS67iAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4zLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvnQurowAAHkdJREFUeJzt3Xt01OW97/H3lxAyQXLhEggaIVR0IxuQS7RSxLu0XqkUa6tWrbW4beul2uOR1tVqbVcvelpk120P3aK2pVtsq93Wosvd47YCC+WEyrYWLFCPPSc2QLgFECaQ5Hv+mEnI5DIzSeb6y+e11qzM7zrPk4RPHp7f8/s95u6IiEj+G5TtAoiISGoo0EVEAkKBLiISEAp0EZGAUKCLiASEAl1EJCAU6CIiAaFAFxEJCAW6iEhADM7kh40aNcqrq6sz+ZEiInlvw4YNu9y9ItF+GQ306upqamtrM/mRIiJ5z8z+lsx+6nIREQkIBbqISEAo0EVEAkKBLiISEAp0EZGAUKCLiARE0oFuZgVm9qaZvdBp/T+b2cHUF01ERHqjN+PQ7wA2A6VtK8ysBihPdaFERPLO0TAc3gvhfZGvh/fC4Q7vZ38Rho5IaxGSCnQzqwIuBb4N3BVdVwA8BFwDXJmuAoqIZIw7NO3vEMidQrk9sLsJ7ebDPZ/XBsHUhbkR6MAS4B6gpMO6LwHPu3u9mfV4oJktAhYBjBs3ro/FFBHpheYjHVr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"metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "df_stack.plot()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>weights</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>性别</th>\n",
       "      <th>患有糖尿病标识</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">0</th>\n",
       "      <th>0</th>\n",
       "      <td>33.838771</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>42.970286</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">1</th>\n",
       "      <th>0</th>\n",
       "      <td>35.522111</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>44.061213</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              weights\n",
       "性别 患有糖尿病标识           \n",
       "0  0        33.838771\n",
       "   1        42.970286\n",
       "1  0        35.522111\n",
       "   1        44.061213"
      ]
     },
     "execution_count": 81,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_stack.stack()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>性别</th>\n",
       "      <th>患有糖尿病标识</th>\n",
       "      <th>weights</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>33.838771</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>42.970286</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>35.522111</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>44.061213</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   性别  患有糖尿病标识    weights\n",
       "0   0        0  33.838771\n",
       "1   0        1  42.970286\n",
       "2   1        0  35.522111\n",
       "3   1        1  44.061213"
      ]
     },
     "execution_count": 83,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_reset = df3.reset_index()\n",
    "df_reset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>患有糖尿病标识</th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>性别</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>33.838771</td>\n",
       "      <td>42.970286</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>35.522111</td>\n",
       "      <td>44.061213</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "患有糖尿病标识          0          1\n",
       "性别                           \n",
       "0        33.838771  42.970286\n",
       "1        35.522111  44.061213"
      ]
     },
     "execution_count": 85,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_pivot = df_reset.pivot(\"性别\", \"患有糖尿病标识\", \"weights\")\n",
    "df_pivot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
